<?php

App::uses('CakeTime', 'Utility');

class ImportController extends AppController {
    var $helpers = array ('Html','Form','Session');
    var $name = 'Import'; // riferimento alla cartella delle view
    //var $modelClass = 'Multa';
    
    // var $components = array('Permission','Acl'); 
    var $components = array('Permission','Acl','ExcelReader'); 
    
    public function beforeFilter(){
		parent::beforeFilter();
        $this->Auth->allow('index');
    }

    public function index() {
		$imports = $this->Import->find("all");
		$this->set("imports",$imports);
    }
	
	public function execute($type, $importId) {
		$import = $this->Import->read(null,$importId);
		try {
			if ($type!=IMPORT_TYPE_DOCENTE && $type!=IMPORT_TYPE_STUDENTE)
				throw new Exception(__("Tipo import non supportato, verificare il primo carattere nel nome del file."));
			$this->{"execute".strtoupper($type)}($import);
			$this->Import->set("dt_import", date("Y/m/d H:i:s"));
			$this->Import->set("status", IMPORT_STATUS_IMPORTED);
		} catch (Exception $e) {
	    	CakeLog::error($e->getMessage());
			$this->Import->set("status", IMPORT_STATUS_ERROR);
			$this->Import->set("error", $e->getMessage());
			$this->Session->setFlash(__("Errore: %s", $e->getMessage()),"default",null,"error");
		}
		$this->Import->save();
		$this->Session->setFlash(__("Importazione terminata."));
        $this->redirect($this->referer());
	}
	
	protected function executeD($import) {
		
		
	}

	protected function executeS($import) {
		$ds = null;
	    try {
		    $this->loadModel('Anagrafica');
		    $this->loadModel('User');
		    $this->loadModel('Studente');
			$ds = $this->User->getDataSource();
			$ds->begin();
			
			$this->inserisciDisciplina($import["Attach"]["original_name"]);
			
			$excel = $this->ExcelReader->read($import["Attach"]["filesystempath"]);
			// pr($excel->sheets);
			for ($i=6; $i < $excel->sheets["0"]["numRows"]; $i++) { 
				$this->inserisciStudente($excel->sheets["0"]["cells"][$i]);
			}
			$ds->commit();
	    } catch (Exception $e) {
	    	if (isset($ds))
	    		$ds->rollback();
			throw $e;
	    }
	}
	
	private function inserisciDisciplina($filename) {
		$ret = split("_", $filename);
		$classe = $ret[1];
		$sezione = $ret[2];
		$ret = split(".", $ret[3]);
		$disciplina = $ret[0];
	    $this->loadModel('Disciplina');
		
	}
	
	private function inserisciStudente($record) {
				$this->Anagrafica->create();
				$this->Anagrafica->set("nome",$record["2"]);
				$this->Anagrafica->set("cognome",$record["1"]);
				$this->Anagrafica->set("data_nascita",CakeTime::format(strtotime($record["3"]),"%Y-%m-%d"));
				$this->Anagrafica->set("sesso",$record["4"]);
				$this->Anagrafica->save();
				// pr($this->Anagrafica->data);
				$this->User->create();
				$this->User->set("anagrafica_id",$this->Anagrafica->id);
				$this->User->set("username",$record["2"].".".$record["1"]);
				$this->User->set("email","");
				$this->User->set("stato","A");
				$this->User->set("group_id",25); // TODO leggere l'id da un parametro
				$this->User->save();
				$this->Studente->create();
				$this->Studente->set("user_id",$this->User->id);
				$this->Studente->set("classe_id",1); //TODO sostituire con la classe corretta
				$this->Studente->save();
	}
	
	public function delete() {
		
	}

}
?>